package com.dz.control;

import com.dz.bean.guanli;
import com.dz.dao.guanliMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;

@Controller
@Slf4j
public class GuanLiControl {
    @Resource
    private guanliMapper guanliMapper;


    @GetMapping(value = "/getGuanLi")
    @ResponseBody
    public List<guanli> getGL(){
        List<guanli> list=guanliMapper.selectAllguanli();

//        String str= JSON.toJSON(list).toString();
        return list;
    }


    @GetMapping(value = "/role-add")
    public String getGL00(){


//        String str= JSON.toJSON(list).toString();
        return "role-add";
    }


    @GetMapping(value = "/selectLikeGuanLi")
    @ResponseBody
    public List<guanli> getGL04(HttpServletRequest request){
        String name=request.getParameter("name");

        List<guanli> list= guanliMapper.selectLikeguanli(name);

//        String str= JSON.toJSON(list).toString();
        return list;
    }
    @GetMapping(value = "/selectLikejuese")
    @ResponseBody
    public List<guanli> getGL05(HttpServletRequest request){
        String juese=request.getParameter("juese");
        guanli g1 =new guanli();
        g1.setJuese(juese);

        List<guanli> list= guanliMapper.selectLikejuese(g1);

//        String str= JSON.toJSON(list).toString();
        return list;
    }



    @GetMapping(value = "/selectLike")
    @ResponseBody
    public List<guanli> getGL06(HttpServletRequest request){
        String name=request.getParameter("username");
        guanli g1=new guanli();
        g1.setAname(name);
        List<guanli> list= guanliMapper.selectLike(g1);

//        String str= JSON.toJSON(list).toString();
        return list;
    }

    @GetMapping(value = "/deleteGuanLi/{id}")
    @ResponseBody
    public int getGL03(@PathVariable("id") Integer id){
       int i= guanliMapper.deleteGuanli(id);

//        String str= JSON.toJSON(list).toString();
        return i;
    }

    @PostMapping(value = "/insertGuanli")
//    @ResponseBody
    public String insertGuanli(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        guanli g1=new guanli();
        Date date=new Date();
        SimpleDateFormat sl=new SimpleDateFormat("yyyy-MM-dd");
        String ss=sl.format(date);
        g1.setJiarushijian(ss);
        String aname=request.getParameter("aname");
        String phone=request.getParameter("phone");
        String email=request.getParameter("email");
        String psw=request.getParameter("password");
        String l1=request.getParameter("like1");
        String l2=request.getParameter("like2");
        String l3=request.getParameter("like3");
        String str="";
        if ("on".equals(l1)) {
            str+="管理员";
        }
        if("on".equals(l2)){
            str+=",编辑人员";
        }
        if("on".equals(l3)){
            str+=",宣传人员";
        }
        g1.setApsw(psw);
        g1.setJuese(str);
        g1.setPhone(phone);
        g1.setAname(aname);
        g1.setEmail(email);

        System.out.println(g1);



//        g1.setName("李六");
//        g1.setAname("user0");
//        g1.setApsw("123456");
//        g1.setEmail("1007881302@qq.com");
//        g1.setPhone("7760172");
//        g1.setJuese("管理者");

        int result=guanliMapper.guanliinsert(g1);
        return "admin-list";

    }
}
